package csel.model.entity;

import csel.model.ObsHelper;
import csel.model.Observer;
import csel.model.items.*;

import java.util.*;

/**
 * A "bag" of the specified item type that will be carried by a Character.
 */
public class Inventory<T extends TakeableItem> implements ItemCollection<T>, java.io.Serializable
{
    /**
     * Change this if we change its structure.
     */
    private static final long serialVersionUID = 6L;

	/* observers */
	private final ObsHelper<Object> observers;

	private List<T> items;
	
	public Inventory()
	{
		observers = new ObsHelper<Object>(this);
		
		items = Collections.synchronizedList(new ArrayList<T>());
	}
	
	public boolean add(T i)
	{
		// TODO: implement maximum carriage?
		
		boolean b = items.add(i);
		notifyO();
		return b;
	}

	public boolean remove(T i)
	{
		boolean b = items.remove(i);
		notifyO();
		return b;
	}

	public boolean contains(T item)
	{
		return items.contains(item);
	}

	public T getItem(int slot) throws ItemIndexOutOfBoundsException
	{
		try{
		return items.get(slot);
		}
		catch(IndexOutOfBoundsException e)
		{
			throw new ItemIndexOutOfBoundsException();
		}
	}
	
	public int size()
	{
		return items.size();
	}

	public void addObserver(Observer<Object> o)
	{
		observers.add(o);
	}

	public boolean removeObserver(Observer<Object> o)
	{
		return observers.remove(o);
	}
	
	protected void notifyO()
	{
		observers.notifyO(null);
	}
}
